<?php

/**
 * 导入管理
 * @copyright   Copyright (c) 2006 - 2010 DZH 友拓软件友拓通
 * @category   	导入管理
 * @package  	Model
 * @author    	wqy
 * @version 	2.1,2012-07-22
*/

class ExcelPublicModel extends AbsExcelPublicModel  {
	
 	  protected $tableName 	= 'product';
	  public $excel_info	=	
	  array( 
	  			'District'			=>array(
			  							'main'				=>'district',//key
			  							'valid'				=>'Basic_addCountry',//验证规则 
			  							'befor_valid'		=>'beforValidDistrict',//前置验证
			  							'replace'	=>array(
			  										'city_name'=>array(
			  																'field'			=>'parent_id',
			  																'tables'		=>'district',
			  																'insert_name'	=>'district_name',
			  																'default'		=>array('to_hide'=>1), 
			  																'valid'			=>'require', 
			  																'after_insert'	=>true, 
			  																),
			  										), 
			  							'default'	=>array('to_hide'=>1,'parent_id'=>0), 
			  							'dd'		=>array(5,2), 
			  							),
			  	'Employee'			=>array(
			  							'main'		=>'employee',//key
			  							'dd'		=>array(7,9,12),//key
			  							'valid'		=>'Basic_addEmployee',//验证规则
			  							'befor_fn'	=>'beforEmployee',//验证规则
			  							'field'		=>array('country_name','district_name'),//插入字段的顺序 
			  							'auto_no'	=>'employee_no', 
			  							'action'	=>'Employee', 
			  							'replace'	=>array(
			  										//部门
			  										'department_name'=>array(
			  																'field'			=>'department_id',
			  																'tables'		=>'department',
			  																'insert_name'	=>'department_name',
			  																'default'		=>array('to_hide'=>1),
			  																'auto_no'		=>'department_no',
			  																'action'		=>'Department'
			  																),
			  										//职务						
			  										'position_name'=>array(
			  																'field'			=>'position_id',
			  																'tables'		=>'position',
			  																'insert_name'	=>'position_name',
			  																'default'		=>array('to_hide'=>1),
			  																'auto_no'		=>'position_no',
			  																'action'		=>'Position'
			  																),						
			  										), 
			  							'default'	=>array('to_hide'=>1), 
			  							),	
			  	'Size'			=>array(
			  							'main'		=>'size',//key
			  							'dd'		=>array(21),//dd
			  							'valid'		=>'Basic_addSize',//验证规则
			  							'field'		=>array('country_name','district_name'),//插入字段的顺序 
			  							'default'	=>array('to_hide'=>1), 
			  							'auto_no'	=>'size_no', 
			  							'action'	=>'Size', 
			  							),	 
			  	'Color'			=>array(
			  							'main'		=>'color',//key
			  							'main'		=>array(20),//key
			  							'valid'		=>'Basic_addColor',//验证规则
			  							'field'		=>array('color_no','color_name'),//插入字段的顺序 
			  							'default'	=>array('to_hide'=>1), 
			  							'auto_no'	=>'color_no', 
			  							'action'	=>'Color', 
			  							),	
			  								 
	  			'Product'			=>array(
			  							'main'		=>'product',//key
			  							'dd'		=>array(20,21,16,14),//dd
			  							'auto_no'	=>'product_no', 
			  							'action'	=>'Product', 
			  							'befor_valid'	=>'beforValidProduct',//插入前的验证方法
			  							'valid'		=>'Basic_addProduct',//验证规则
			  							'field'		=>array('product_no','product_name','class_name','factory_name','color_name','size_name'),//插入字段的顺序
			  							//产品编号	产品名称	类别名称	厂家名称	颜色	尺码	零售单价	其他单价

			  							'after_fn'	=>'afterProduct',//插入后的执行的方法
			  							'explode'	=>array('color_name','size_name','class_name'), //需要切割的字段 
			  							'replace'	=>array( 
			  										'color_name'=>array(
			  																'field'			=>'color_id',
			  																'tables'		=>'color',
			  																'insert_name'	=>'color_name',
			  																'default'		=>array('to_hide'=>1),
			  																'auto_no'		=>'color_no',
			  																'action'		=>'Color'
			  																),
			  										'size_name'=>array(
			  																'field'			=>'size_id',
			  																'tables'		=>'size',
			  																'insert_name'	=>'size_name',
			  																'default'		=>array('to_hide'=>1),
			  																'auto_no'		=>'size_no',
			  																'action'		=>'Size'
			  																),	
			  										'factory_name'=>array(
			  																'field'			=>'factory_id',
			  																'tables'		=>'company',
			  																'insert_name'	=>'comp_name',
			  																'select'		=>true,
			  																'default_value'	=>'',
//			  																'default'		=>array('comp_type'=>2,'to_hide'=>1),
//			  																'auto_no'		=>'comp_no',
//			  																'action'		=>'Factory'
			  																),									 				
			  										'class_name'=>array(
			  																'field'			=>'product_class_id',
			  																'tables'		=>'product_class',
			  																'insert_name'	=>'class_name',
			  																'default'		=>array('to_hide'=>1),
			  																'auto_no'		=>'class_no',
			  																'action'		=>'ProductClass',
			  																'explode_function'		=>'explodeClassName',
			  																),						
			  										), 
			  							'max_row'	=>2999, 			
			  							'default'	=>array('to_hide'=>1,'orders_norms'=>1), 
			  							),
	  			'Client'			=>array(
			  							'main'		=>'client',//key
			  							'dd'		=>array(14),//key
			  							'valid'		=>'Basic_addClient',//验证规则 
			  							'auto_no'	=>'comp_no',
			  							'action'	=>'Client',
			  							'replace'	=>array( 
			  										'country_name'=>array(
			  																'field'			=>'country_id',
			  																'tables'		=>'district',
			  																'insert_name'	=>'district_name',   
			  																'select'		=>true,
			  																), 
			  										'city_name'=>array(
			  																'field'			=>'city_id',
			  																'tables'		=>'district',
			  																'insert_name'	=>'district_name',   
			  																'select'		=>true,
			  																), 							
			  										), 
			  							'befor_fn'	=>'beforClient',//验证规则
			  							'max_row'	=>2999, 	
			  							'default'	=>array('to_hide'=>1,'comp_type'=>1,), 
			  							),
			  	'Factory'			=>array(
			  							'main'		=>'factory',//key
			  							'dd'		=>array(14),//key
			  							'valid'		=>'Basic_addFactory',//验证规则
			  							'auto_no'	=>'comp_no',
			  							'action'	=>'Factory',
			  							'befor_fn'	=>'beforFactory',//验证规则
			  							'replace'	=>array( 
			  										'currency_name'=>array(
			  																'field'			=>'currency_id',
			  																'tables'		=>'currency',
			  																'insert_name'	=>'currency_no',  
			  																'default_value'	=>-1,
			  																'select'		=>true,
			  																), 	
			  										'country_name'=>array(
			  																'field'			=>'country_id',
			  																'tables'		=>'district',
			  																'insert_name'	=>'district_name',   
			  																'select'		=>true,
			  																), 
			  										'city_name'=>array(
			  																'field'			=>'city_id',
			  																'tables'		=>'district',
			  																'insert_name'	=>'district_name',   
			  																'select'		=>true,
			  																), 									
			  																
			  										), 
			  							
			  							'default'	=>array('to_hide'=>1,'comp_type'=>2,), 
			  							),	
			  	'OtherCompany'		=>array(
			  							'main'		=>'other_company',//key
			  							'dd'		=>array(14),//key
			  							'valid'		=>'Basic_addOtherCompany',//验证规则
			  							'auto_no'	=>'comp_no',
			  							'action'	=>'OtherCompany',
			  							'befor_fn'	=>'beforOtherCompany',//验证规则
			  							'replace'	=>array( 
			  										'currency_name'=>array(
			  																'field'			=>'currency_id',
			  																'tables'		=>'currency',
			  																'insert_name'	=>'currency_no',  
			  																'default_value'	=>0,
			  																'select'		=>true,
			  																), 	
			  										'country_name'=>array(
			  																'field'			=>'country_id',
			  																'tables'		=>'district',
			  																'insert_name'	=>'district_name',   
			  																'select'		=>true,
			  																), 
			  										'city_name'=>array(
			  																'field'			=>'city_id',
			  																'tables'		=>'district',
			  																'insert_name'	=>'district_name',   
			  																'select'		=>true,
			  																), 									
			  																
			  										), 
			  							'default'		=>array('to_hide'=>1,'comp_type'=>4,),  
			  							),	  
	  			'InitStorage'		=>array(
			  							'main'			=>'initStorage',//key  
			  							'table_name'	=>'initStorageDetail',//表名
			  							'befor_fn'		=>'beforInistock',//插入前的执行的方法
//			  							'befor_valid'	=>'beforValidStock',//插入前的验证方法
			  							'max_row'	=>2999, 	
			  							'field'		=>array('product_no','quantity','capability','dozen','price','quantity_state'),//插入字段的顺序  
			  							'replace'	=>array(
			  										'product_no'=>array(
			  																'field'			=>'product_id',
			  																'tables'		=>'product',
			  																'insert_name'	=>'product_name', 
			  																'select'		=>true, 
			  																),	
			  										'size_name'=>array(
			  																'field'			=>'size_id',
			  																'tables'		=>'size',
			  																'insert_name'	=>'size_name', 
			  																'select'		=>true, 
			  																),	
			  										'color_name'=>array(
			  																'field'			=>'color_id',
			  																'tables'		=>'color',
			  																'insert_name'	=>'color_name', 
			  																'select'		=>true, 
			  																),															 
			  										), 
			  							//	产品号、箱数、每箱数量、每包数量、单价、是否尾箱【以上必填】
			  							),
			  							
			  	'Stocktake'		=>array(  
			  							'main'			=>'stocktake',//key  
			  							'table_name'	=>'stocktakeDetail',	//表名
			  							'max_row'		=>2999, 	
			  							'befor_fn'		=>'beforStocktake',		//插入前的执行的方法
//			  							'befor_valid'	=>'beforValidStock',	//插入前的执行的方法 
			  							'replace'		=>array(
			  										'product_no'=>array(
			  																'field'			=>'product_id',
			  																'tables'		=>'product',
			  																'insert_name'	=>'product_name', 
			  																'select'		=>true, 
			  																),	
			  										'size_name'=>array(
			  																'field'			=>'size_id',
			  																'tables'		=>'size',
			  																'insert_name'	=>'size_name', 
			  																'select'		=>true, 
			  																),	
			  										'color_name'=>array(
			  																'field'			=>'color_id',
			  																'tables'		=>'color',
			  																'insert_name'	=>'color_name', 
			  																'select'		=>true, 
			  																),															 
			  										), 
			  							'field'		=>array('country_name','district_name'),//插入字段的顺序  
			  							),		 
	  			//init_storage_detail
	  			'ClientIni'		=>array(
			  							'main'		=>'client_paid',//key
			  							'valid'		=>'Funds_addClientIni',//验证规则
			  							'max_row'	=>2999, 	
			  							'table_name'=>'paid_detail',//验证规则
			  							'field'		=>array('comp_name','basic_name','paid_date','currency_no','money','comments'),//插入字段的顺序
			  							'befor_fn'	=>'beforFundclient',//插入前的执行的方法
			  							'replace'	=>array(
			  										'client_name'=>array(
			  																'field'			=>'comp_id',
			  																'tables'		=>'company',
			  																'insert_name'	=>'comp_name', 
			  																'select'		=>true, 
			  																),			
			  										'company_name'=>array(
			  																'field'			=>'basic_id',
			  																'tables'		=>'basic',
			  																'insert_name'	=>'basic_name',
			  																'select'		=>true, 
			  																),	
			  										'currency_name'=>array(
			  																'field'			=>'currency_id',
			  																'tables'		=>'currency',
			  																'insert_name'	=>'currency_no',
			  																'select'		=>true, 
			  																),	 				
			  										),  
			  							'default'	=>array('to_hide'=>1,'paid_type'=>1,'object_type'=>101,'paid_income_type'=>'-1','comp_type'=>1,), 
			  							),	
			  	'FactoryIni'		=>array(
			  							'main'		=>'factory_paid',//key
			  							'valid'		=>'Funds_addClientIni',//验证规则 
			  							'max_row'	=>2999, 	
			  							'befor_fn'	=>'beforFundfactory',//插入前的执行的方法
			  							'replace'	=>array(
			  										'comp_name'=>array(
			  																'field'			=>'comp_id',
			  																'tables'		=>'company',
			  																'insert_name'	=>'comp_name', 
			  																'select'		=>true, 
			  																),		 
			  										'currency_name'=>array(
			  																'field'			=>'currency_id',
			  																'tables'		=>'currency',
			  																'insert_name'	=>'currency_no',
			  																'select'		=>true, 
			  																),	 				
			  										),  
			  							'default'	=>array('to_hide'=>1,'basic_id'=>1,'paid_type'=>1,'paid_income_type'=>'-1','object_type'=>201,'comp_type'=>2,), 
			  							),		 
	  );
	  
	  
	  /**
	   * 导出
	   *
	   */
	  public function getOutPut(){
	  	$data = include(THINK_PATH.'Conf/outputExcel.php');
	  	if($_GET['state']=='ProductClass'){
	  		$level	 = C('PRODUCT_CLASS_LEVEL');
			$to_hide = intval($_SESSION[$_GET['rand']]['stohide']);
			if ($to_hide==-2) { $to_hide	= '1'; }
	  		if ($level==1) {
				$where .= ' and (a.to_hide='.$to_hide.')';
			}elseif ($level==2) {
				$where .= ' and (a.to_hide='.$to_hide.' or b.to_hide='.$to_hide.')';
			}elseif ($level==3) {
				$where .= ' and (a.to_hide='.$to_hide.' or b.to_hide='.$to_hide.' or c.to_hide='.$to_hide.')';
			}elseif ($level==4) {
				$where .= ' and (a.to_hide='.$to_hide.' or b.to_hide='.$to_hide.' or c.to_hide='.$to_hide.' or d.to_hide='.$to_hide.')';
			}
			$sql	= 'SELECT a.class_name as level1,b.class_name as level2,c.class_name as level3,d.class_name as level4 FROM `product_class`  as a 
						left join product_class as b on(a.id=b.parent_id and b.class_level=2)
						left join product_class as c on(b.id=c.parent_id and c.class_level=3)
						left join product_class as d on(c.id=d.parent_id and d.class_level=4)
						where '.getWhere($_SESSION[$_GET['rand']]).$where.' and a.class_level=1 ORDER BY a.id desc';
			$list	= _formatList($this->db->query($sql));
	  	}else{
	  		$list	= _formatList($this->db->query($data[$_GET['state']]['sql']));	
	  	}
	  	$list['fields']	= $data[$_GET['state']]['title'];
	  	$list['value']	= $data[$_GET['state']]['content'];
	  	return $list;
	  }
	  
}
?>